package cn.lijigou.shopping.service;

import cn.lijigou.shopping.HomeActivity;
import cn.lijigou.shopping.MainTabActivity;
import cn.lijigou.shopping.OrdersActivity;
import cn.lijigou.shopping.ShoppingsActivity;
import cn.lijigou.shopping.domain.Advertising;
import cn.lijigou.shopping.domain.Order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

public class MobileNetworkListener extends BroadcastReceiver {
    
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		
        String action=intent.getAction();
        if(action.equals("android.net.conn.CONNECTIVITY_CHANGE")){
        	if(getNetConnectState(context)){
        		if(MainTabActivity.firstReFresh){
	        		Toast.makeText(context, "亲，连接到网络了....", 1).show();
        		}else{
        			MainTabActivity.firstReFresh=true;
        		}
        		//防止断网后广告没有下载下来，连上网络后继续下载
//        		if(!(HomeActivity.refreshAds)){
//        			for(Advertising advertising:HomeActivity.adsList){
//        				if(advertising.getDrawable()==null){
////        					ServerService.sirectLoadImageFromUrl(advertising ,context);
//        				}
//        			}
//        		} 
        		
        		
        		SharedPreferences sp = context.getSharedPreferences("info", Context.MODE_PRIVATE);
        		String LBSlng=sp.getString("LBSlng", "");
        		String LBSlat=sp.getString("LBSlat", "");
        	 if(MSystem.mLocation==null){
//        		LocationService locationService = new LocationService(context);
//        		locationService.start();
        	  }
        		
        	}else{
        			Toast.makeText(context, "亲，网络存在问题哦..", 1).show();
        	}
        	
        }
        
	}
	
	public boolean getNetConnectState(Context context) {
		boolean netConnect = false;
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo infoM = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (info.isConnected() || infoM.isConnected()) {
			netConnect = true;
		}
		return netConnect;
	}

}
